﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCPnuts.ISkyDiskFileSystem.Interfaces;
using CCPnuts.Pack;
using CCPnuts.ISkyDiskFileSystem.Exceptions;

namespace TestSkyDiskFileSystem
{
    class SkyElementContainer : SkyElement, I_SkyElementContainer
    {
        public SkyElementContainer(PackedInfo info) : base(info) { }

        #region I_SkyElementContainer Members

        public I_Uploader UploadFile(string path)
        {
            return new Uploader { TotalToSend = SkyFile.FileContent.Length };
        }

        public void Delete(I_SkyElement element)
        {
            Children.ToList().Remove(element);
        }

        public void DeleteFile(string fileName)
        {
            var res = from item in Children where item is SkyFile && item.Name == fileName select item;
            if (res.Count() != 1)
            {
                throw new SkyElementNotFoundException(String.Format("The File : {0} can not be found.", fileName));
            }
            Children.ToList().Remove(res.First());
        }

        public void DeleteDir(string dirName)
        {
            var res = from item in Children where item is SkyDirectory && item.Name == dirName select item;
            if (res.Count() != 1)
            {
                throw new SkyElementNotFoundException(String.Format("The Directory : {0} can not be found.", dirName));
            }
            Children.ToList().Remove(res.First());
        }

        public void CreateDir(string dirName)
        {
            var res = from item in Children where item is SkyDirectory && item.Name == dirName select item;
            if (res.Count() > 0)
            {
                throw new SkyElementCreateException(String.Format("There is already a Directory named : {0}", dirName));
            }
            Children.ToList().Add(new SkyDirectory { Name = dirName, Children = new SkyElementCollection(), Parent = this });
        }

        public I_SkyElementCollection Children { get; set; }

        #region Optional & Not Implement

        public I_SkyDirectory this[string path]
        {
            get { throw new NotImplementedException(); }
        }

        public I_SkyFile this[string path, string fileName]
        {
            get { throw new NotImplementedException(); }
        }

        public I_SkyDirectory GetDir(string dirName)
        {
            throw new NotImplementedException();
        }

        public bool HasDir(string dirName)
        {
            throw new NotImplementedException();
        }

        public I_SkyFile GetFile(string fileName)
        {
            throw new NotImplementedException();
        }

        public bool HasFile(string fileName)
        {
            throw new NotImplementedException();
        }

        public bool UploadDirectory(string dirpath)
        {
            throw new NotImplementedException();
        }

        #endregion

        #endregion
    }
}
